package com.example.myproject.service.serviceImpl;


import com.example.myproject.model.Function;
import com.example.myproject.repository.FunctionRepository;
import com.example.myproject.service.FunctionService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.Optional;
import java.util.Set;

@Service
public class FunctionServiceImpl implements FunctionService {
    private static final Logger log = LoggerFactory.getLogger(FunctionServiceImpl.class);
    @Autowired
    private FunctionRepository functionRepository;

    // Retrieve functions by a set of IDs
    @Override
    public Set<Function> findFunctionsByIds(Set<Long> ids) {
        log.debug("查询权限，权限ID集：{}", ids);
        Set<Function> functions = new HashSet<>(functionRepository.findByIdIn(ids));
        log.info("找到的权限数量：{}", functions.size());
        return functions;
    }

    // Find a function by ID
    @Override
    public Optional<Function> findById(int id) {
        log.debug("通过ID查找权限，ID：{}", id);
        Optional<Function> function = functionRepository.findById(id);
        function.ifPresent(f -> log.info("找到权限：{}", f));
        if (!function.isPresent()) {
            log.warn("未找到ID为 {} 的权限", id);
        }
        return function;
    }

    // Save a function entity
    @Override
    public Function save(Function function) {
        log.debug("保存权限，权限详情：{}", function);
        Function savedFunction = functionRepository.save(function);
        log.info("权限保存成功，ID：{}", savedFunction.getId());
        return savedFunction;
    }

    // Delete a function by ID
    @Override
    public void deleteById(int id) {
        log.debug("删除权限，权限ID：{}", id);
        functionRepository.deleteById(id);
        log.info("ID为 {} 的权限已删除", id);
    }
}
